package com.kent.hellokotlin.cClassAndObject

//泛型

//与 Java 类似，Kotlin 中的类也可以有类型参数：
class Box<T>(val t: T)

val box = Box(1)

interface Out<out T> {

    val t: T
    //var t1:T //not allow var

    fun outMethod(): T

    //fun inMethod(t:T) // not allow here
}

interface In<in T> {

    //val t:T // not allow here
    //var t:T // not allow here

    //fun outMethod():T // not allow here

    fun inMethod(t: T)
}

fun copy(from: Array<out Any>, to: Array<Any>) {
    assert(from.size == to.size)
    for (i in from.indices) {
        to[i] = from[i]
    }
}

fun fooCopy() {
    val ints: Array<Int> = arrayOf(1, 2, 3)
    val any = Array<Any>(3) { "" }
    copy(ints, any)
}


fun <T> singleItemToList(item: T): List<T> {
    return listOf(item)
}

fun <T> T.baseToString(): String {
    return "baseToString: ${this.toString()}"
}

private fun foo1() {
    val singleItemToList: List<Int> = singleItemToList(1)
    for (i in singleItemToList) {
        println(i)
    }
    println("this is origin str".baseToString())
}

fun main() {
    foo1()
}

fun <T> copyWhenGreater(list: List<T>, threshold: T): List<String> where T : CharSequence, T : Comparable<T> {
    return list.filter { it > threshold }.map { it.toString() }
}